# 元组的操作

new_tuple = ("jackie", 28, 98.7, "english")
print(*new_tuple)

name, age, *rest = new_tuple
print(f"name={name},age={age},rest={rest}")


# 从列表创建
list_data = [1, 2, 3]
tuple_from_list = tuple(list_data)
print(tuple_from_list)  # (1, 2, 3)

# 从字符串创建
tuple_from_string = tuple("abc")
print(tuple_from_string)  # ('a', 'b', 'c')

# 创建空元组
another_empty_tuple = tuple()
print(another_empty_tuple)  # ()


tuple_one = (1, 2, 3, 4)
tuple_two = (1, 2, 3, 4)
print(f"id(tuple_one)={id(tuple_one)},id(tuple_two)={id(tuple_two)}")


fruits = ("apple", "banana", "cherry")


def printTuple(tuple_params):
    for tuple_ele in tuple_params:
        print(f"tuple_ele={tuple_ele}")

    return [ele for ele in tuple_params if ele == "banana"]


new_list = printTuple(fruits)

print(f"遍历元组并返回新的列表={new_list}")


# 基本解包
person = ("Alice", 25, "Engineer")
name, age, job = person
print(name, age, job)  # Alice 25 Engineer

# 使用 * 收集多余元素
numbers = (1, 2, 3, 4, 5)
first, second, *rest = numbers
print(f"1-first={first},second={second},rest={rest}")
*first, second, last = numbers
print(f"2-first={first},second={second},last={last}")  # first=[1,2,3],second=4,last=5


# 交换变量（经典用法）
a, b = 10, 20
a, b = b, a  # 实际上是 (a, b) = (20, 10)
print(a, b)  # 20 10


def operate(x, y):
    sum = x + y
    minius = x - y
    mul = x * y
    div = x / y
    return sum, minius, mul, div


tuple_result_of_two_number_operate = operate(10, 20)
print(
    f"tuple_result_of_two_number_operate的类型={type(tuple_result_of_two_number_operate)}"
)


t4 = (100,)
print(f"t4的类型={type(t4)}")
